// pages/regi/regi.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        registerstate:'true',
        registeriphone:'',
        registerpassword:'',
        registerAgainpassword:''
    },

    regist: function (e) {
        var that = this
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        if (that.data.username == '') {
          wx.showModal({
            title: '温馨提示',
            content: '请输入用户名!',
            showCancel: false,
            success(res) { }
          })
        } else if (that.data.registeriphone == '') {
          wx.showModal({
            title: '温馨提示',
            content: '请输入手机号!',
            showCancel: false,
            success(res) { }
          })
        } else if (that.data.registeriphone.length != 11) {
          wx.showModal({
            title: '温馨提示',
            content: '手机长度有误，请重新输入!',
            showCancel: false,
            success(res) { }
          })
        } else if (!myreg.test(that.data.registeriphone)) {
          wx.showModal({
            title: '温馨提示',
            content: '请输入正确的手机号码！',
            showCancel: false,
            success(res) { }
          })
        } else if (that.data.registerpassword == '') {
          wx.showModal({
            title: '温馨提示',
            content: '请输入密码',
            showCancel: false,
            success(res) { }
          })
        } else if (that.data.registerAgainpassword == '') {
          wx.showModal({
            title: '温馨提示',
            content: '请输入确认密码',
            showCancel: false,
            success(res) { }
          })
        } else if (that.data.registerAgainpassword != that.data.registerpassword) {
          wx.showModal({
            title: '温馨提示',
            content: '两次输入密码不一致!',
            showCancel: false,
            success(res) { }
          })
        } else {
          console.log("success")
          wx.login({
            success: function(res) {
                var code = res.code; //发送给服务器的code 
                wx.getUserInfo({
                    success: function(res) {
                        var userNick = res.userInfo.nickName; //用户昵称 
                        var avataUrl = res.userInfo.avatarUrl; //用户头像地址 
                        var gender = res.userInfo.gender; //用户性别
                        console.log(avataUrl)
                        that.setData({
                            nick: userNick,
                            avataUrl: avataUrl
                        }) 
                        if(code) {
                            wx.request({
                                url: 'http://localhost/wxLogin.php',
                                //服务器的地址，现在微信小程序只支持https请求，所以调试的时候请勾选不校监安全域名
                                data: {
                                    code: code,
                                    nick: userNick,
                                    avaurl: avataUrl,
                                    sex: gender,
                                },
                                header: {
                                    'content-type': 'application/json'
                                },
                                success: function(res) {
                                    console.log(res.data);
                                    wx.setStorageSync('nick', res.data.nick); //将获取信息写入本地缓存 
                                    wx.setStorageSync('openid', res.data.openid);
                                    wx.setStorageSync('imgUrl', res.data.imgUrl);
                                    wx.setStorageSync('sex', res.data.sex);
                                }
                            })
                        } else {
                            console.log("获取用户登录态失败！");
                        }
                    }
                })
            },
            fail: function(error) {
                console.log('login failed ' + error);
            }
        })
    }
    },


    inputiphone2:function (e) {
        this.data.registeriphone = e.detail.value
      },
    
      inputpassword2: function (e) {
        this.data.registerpassword = e.detail.value
      },
      inputAgainpassword2: function (e) {
        this.data.registerAgainpassword = e.detail.value
      },
      inputAgainpassword2: function (e) {
        this.data.registerAgainpassword = e.detail.value
      },
      regist: function (e) {
        this.data.registerAgainpassword = e.detail.value
      },
    //注册手机号判断
   /* inputiphone2:function(e){
    console.log(e.detail.value)
    var that=this;
    var m=e.detail.value
    if(m!=''&&(m.length==11)){
        that.setData({
            registeriphone:m
        })
    
    if(that.data.registerpassword!=''&&that.data.registerAgainpassword!=''){
        that.setData({
            registerstate:false
        })
    }
    else{
        that.setData({
            registerstate:true
        })
    }
}
    else{
        that.setData({
            registerstate:true
        })
    
}
},
    inputpassword2:function(e){
    console.log(e.detail.value)
    var that=this;
    var m=e.detail.value
    if(m!=''){
        that.setData({
            registerpassword:m
        })
        if(that.data.registeriphone!=''&&(that.data.registeriphone.length==11)&&(e.detail.registerpassword==that.data.registerAgainpassword))
        {
            that.setData({
                registerstate:false
            })
        }else{
            that.setData({
                registerstate:true
            })
        }

    }
    else{
        that.setData({
            registerstate:true
        })
    }
    },
    inputAgainpassword2:function(e){
        console.log(e.detail.value)
        var that=this;
        var m=e.detail.value
        if(m!=''){
            that.setData({
                registerAgainpassword:m
            })
            if(that.data.registeriphone!=''&&(that.data.registeriphone.length==11)&&(that.data.registerpassword==e.detail.registerAgainpassword))
            {
                that.setData({
                    registerstate:false
                })
            }else{
                that.setData({
                    registerstate:true
                })
            }
    
        }
        else{
            that.setData({
                registerstate:true
            })
        }
        }, 
        */
    
        formSubmit:function (data) {
        console.log(data.detail.value)  
        var username = data.detail.value.username;
        var pwd1 = data.detail.value.pwd1;
        var pwd2 = data.detail.value.pwd2;
        console.log(username);
        if(username==''){
            wx.showToast({
                title:'请输入用户名',
                icon:'error',
                duration:2000
            })
        }else if(pwd1==''){
            wx.showToast({
                title:'请输入密码',
                icon:'error',
                duration:2000
            })
        }else if(pwd1!=pwd2){
            wx.showToast({
                title:'两次密码不同',
                icon:'error',
                duration:2000
            })
        }else{
          wx.request({
            url: 'http://www.caibier.com/admin/index.php/home/Api/zhuce',
            method:'get',
            //服务器的地址，现在微信小程序只支持https请求，所以调试的时候请勾选不校监安全域名
            data: {
              username:username,
              password:pwd1,
            },
            success: function(res) {
              if(res.data.status =='200'){
                wx.setStorageSync('status', res.data.status); //将获取信息写入本地缓存 
                wx.showToast({
                  title:res.data.msg,
                  icon:'success',
                  duration:4000,
                  success:function(res){
                        wx.navigateTo({
                        url: '../user_page/user_page',
                        })
                      }
              })
              }else{
                wx.showToast({
                  title:res.data.msg,
                  icon:'error',
                  duration:2000
              })
              }
            }
        })
        }
       
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '用户注册',
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})